//
//  ShopBooksViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/19.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "ShopBooksViewController.h"
#import "ShopBookHeader.h"
#import "ShopBooksCell.h"
#import "TranferDetailFinishViewController.h"
#import "SaleDetailFinishViewController.h"
#import "ShopBookModel.h"

@interface ShopBooksViewController ()

/**
 headerView
 */
@property (nonatomic , strong) ShopBookHeader *headerView;


@property (nonatomic, strong) ShopBookModel *model;


@end

@implementation ShopBooksViewController

#pragma mark - view life cycle

- (void)setupUI{
    
    self.view.backgroundColor = KWhiteColor;
    
    {
        self.baseNavView.navTitleLabel.text = @"店铺账本";
        self.baseNavView.navTitleLabel.textColor = KWhiteColor;
        self.baseNavView.backgroundColor = KClearColor;
        self.baseNavView.line.hidden = YES;
    }
    
    {
        [self.baseNavView.leftNavBtn setImage:[UIImage imageNamed:@"nav_white"] forState:UIControlStateNormal];
        [self.headerView addSubview:self.baseNavView];
    }
    
    {
        [self.view addSubview:self.headerView];
    }
    
}


- (void) setupLayout{
    
    {
        self.tableView.height = KScreenHeight - self.headerView.height;
        self.tableView.mj_y = self.headerView.bottom;
    }
    
    {
        CGFloat gap = (self.tableView.height - self.reloadBtn.height)/2.0 + self.headerView.height/2.0;
        self.reloadBtn.mj_y = gap;
        self.reloadLab.mj_y = self.reloadBtn.bottom + 20;
    }
}


//店铺账本 Yes
- (void)initData{
    
    @weakify(self);
    [[[MeatShopApi sharedInstance] getMyShopBookByShopId:@1] subscribeNext:^(ShopBookModel * _Nullable model) {
        
        @strongify(self);

        self.model = model;
        self.headerView.saleLab.text = [NSString stringWithFormat:@"%@", model.shopAccountBookMoney];
        self.headerView.profitsLab.text = [NSString stringWithFormat:@"%@", model.AccumulatedProfit];
        
        self.model = model;
        if (self.isRefresh) {
            [self endRefresh];
        }
        
        if (self.model.GoodsOrderList.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }
        
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
}

//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.model.GoodsOrderList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 84;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShopBooksCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"ShopBooksCell"];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SaleDetailFinishViewController *vc = [[SaleDetailFinishViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - getter and setter

- (void)registerTableViewCell
{
    self.tableView.separatorColor = mIconLineColor;
    self.tableView.mj_footer.hidden = YES;
    [self.tableView registerClass:[ShopBooksCell class] forCellReuseIdentifier:@"ShopBooksCell"];
}


- (ShopBookHeader *)headerView {
    
    if (!_headerView) {
        _headerView = [[ShopBookHeader alloc] init];
    }
    return _headerView;
}


- (ShopBookModel *)model{
    
    if (!_model) {
        _model = [[ShopBookModel alloc] init];
    }
    return _model;
}

#pragma mark override methods

- (void)loadNoData{
    
    [super loadNoData];
    [self.reloadBtn setImage:[UIImage imageNamed:@"meat_book_blank"] forState:UIControlStateNormal];
    self.reloadLab.text = @"暂无数据\n去养头猪赚钱吧";
    self.headerView.hidden = NO;
}

- (void)loadErrorData
{
    [super loadErrorData];
    [self.reloadBtn setImage:[UIImage imageNamed:@"meat_nomal_blank"] forState:UIControlStateNormal];
    self.headerView.hidden = YES;
}

- (void)endRefresh{
    [super endRefresh];
    self.headerView.hidden = NO;
    
}


@end
